import authList from "../../mock/authList";
import { LOGIN } from '../actionTypes/userActionType';
import { loginApi } from '../../http/api/userApi';

// 同步 - 类似 vuex - mutation
export const loginAction = (payload) => (
  {
    type: LOGIN,
    payload
  }
)

// 异步的 - 类似 vuex 的 actions
// export const loginActionAsync = function (paylod) {
//   return function (dispatch) {
//     loginApi(paylod)
//       .then(res => {
//         if (res.data.code == '200') {
//           dispatch(loginAction(res.data.data))
//         } else {
//           console.log('登陆失败：', res.data);
//         }
//       })
//   }
// }

// 异步的回调处理
export const loginActionAsync = function (paylod) {
  return function (dispatch) {
    return new Promise((resolve, reject) => {
      loginApi(paylod)
        .then(res => {
          if (res.data.code == '200') {
            dispatch(loginAction(res.data.data))
            resolve()
          } else {
            console.log('登陆失败：', res.data);
            reject(res.data.msg)
          }
        })
    })
  }
}

export const loginAsync = (data) => {
  return (dispatch) => {
    return new Promise((resolve, reject) => {
      // 模拟异步
      setTimeout(() => {
        dispatch(loginAction(data));
        resolve()
      }, 500);
    })
  };
};
